package it.ap.sa.core.util;

import java.util.List;

import com.vividsolutions.jts.geom.Geometry;


/**
 * utility per convertire una lista in featureCollection
 */
public class FeatureCollectionUtils {

	@SuppressWarnings("unchecked")
	public static <T> FeatureCollection convertList(List<T> lista, FeatureConverter<T> fconverter) throws Exception {
		FeatureCollection fc = new FeatureCollection();
		if (null != lista) {
			for (Object obj : lista) {
				fc.addFeature(fconverter.encode((T) obj));
			}
		}
		return fc;
	}
	
	public static <T> FeatureCollection convertObject(T object, FeatureConverter<T> fconverter) throws Exception {
		FeatureCollection fc = new FeatureCollection();
		if (null != object) {
				fc.addFeature(fconverter.encode((T) object));
		}
		return fc;
	}
	
	public static <T> FeatureCollection convertList(List<T> lista,String geomProp ) throws Exception{
		SimpleFeatureConverter<T> sfc = new SimpleFeatureConverter<T>(geomProp);
		return convertList(lista, sfc);
	}
	
	public static <T> FeatureCollection convertObject(T object,String geomProp ) throws Exception{
		SimpleFeatureConverter<T> sfc=new SimpleFeatureConverter<T>(geomProp);
		return convertObject(object, sfc);
	}
	
	/**
	 * build {@link FeatureCollection} from list of geometry
	 * @param lista
	 * @return
	 * @throws Exception
	 */
	public static FeatureCollection buildFromGeometries(List<Geometry> lista) throws Exception {
		FeatureCollection fc = new FeatureCollection();
		if (null != lista) {
			for (Geometry obj : lista) {
				Feature f = new Feature();
				f.setGeometry(obj);
				fc.addFeature(f);
			}
		}
		return fc;
	}
	
}
